﻿using System;
using System.Collections.Generic;
using System.Text;
using DBSourceToolsLib;
using System.Windows.Forms;
using DBSourceToolsLib.Composite;

namespace DBSourceTools
{
    internal class DBHelper
    {
        // These functions are called from both DBExplorer and MainForm

        internal void LoadFromDatabase(System.Windows.Forms.Form oForm, string strDbName)
        {
            try
            {
                SourceDatabase oSourceDb = UserSettings.Instance().getSourceDatabase(
                    strDbName
                );

                DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                    new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDatabase);

                oSourceLoader.Execute(ref oSourceDb, null);
                if (oSourceLoader.HasErrors())
                    throw new Exception(oSourceLoader.GetErrorMessage());

                DBSourceToolsLib.SourceDatabaseWriter oSourceWriter =
                    new SourceDatabaseWriter(oForm, DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk
                        , UserSettings.Instance().DatabaseProject);
                oSourceWriter.Execute(ref oSourceDb, null);

                if (oSourceWriter.HasErrors())
                    throw new Exception(oSourceWriter.GetErrorMessage());
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(oForm);

            }

        }

        internal void LoadFromDisk(System.Windows.Forms.Form oForm, string strDbName)
        {
            try
            {
                SourceDatabase oSourceDb = UserSettings.Instance().getSourceDatabase(
                    strDbName);

                DBSourceToolsLib.SourceDatabaseLoader oSourceLoader =
                    new DBSourceToolsLib.SourceDatabaseLoader(oForm,
                        DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadFromDisk);

                oSourceLoader.Execute(ref oSourceDb, null);
                if (oSourceLoader.HasErrors())
                    throw new Exception(oSourceLoader.GetErrorMessage());
            }
            catch (Exception ex)
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(ex.ToString());
                oError.ShowDialog(oForm);
            }

        }

        internal void SaveProject(System.Windows.Forms.Form oForm, bool bRefreshData)
        {
            UserSettings.Instance().DatabaseProject.WriteProjectFile();

            DBSourceToolsLib.SourceDatabaseWriter oWriter = new SourceDatabaseWriter(oForm,
                DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk,
                UserSettings.Instance().DatabaseProject);

            for (int i = 0; i < UserSettings.Instance().DatabaseProject.SourceDatabases.Count; i++)
            {
                SourceDatabase oSource = UserSettings.Instance().DatabaseProject.SourceDatabases[i];
                oSource.ScriptOptions.RefreshData = bRefreshData;
                oWriter.Execute(ref oSource, null);
                if (oWriter.HasErrors())
                {
                    DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                        new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(oWriter.ErrorMessage);
                    oError.ShowDialog(oForm);
                    break;
                }
            }

        }
        internal void NewDeploymentTarget(System.Windows.Forms.Form oForm)
        {
            DBSourceToolsLib.DatabaseProjectUI.NewDatabaseForm oNewDatabaseForm =
                new DBSourceToolsLib.DatabaseProjectUI.NewDatabaseForm(false, false);
            if (oNewDatabaseForm.ShowDialog(oForm) == DialogResult.OK)
            {
                SourceDatabase oSource = UserSettings.Instance().getSourceDatabase(
                    UserSettings.Instance().CurrentDbNickName
                    );
                oSource.AddTargetDatabase(
                    oNewDatabaseForm.GetDatabaseObject(), true);
            }

        }

        internal void SaveSourceProject(System.Windows.Forms.Form oForm, bool bRefreshData, TreeNodeTag oCurrentTreeNodeTag)
        {
            // TODO : COMPLETE 1.2.0.8 Write data for only one source database
            UserSettings.Instance().DatabaseProject.WriteProjectFile();

            DBSourceToolsLib.SourceDatabaseWriter oWriter = new SourceDatabaseWriter(oForm,
                DBSourceToolsLib.Commands.DatabaseCommandTypes.WriteToDisk,
                UserSettings.Instance().DatabaseProject);

            SourceDatabase oSourceDb = UserSettings.Instance().getSourceDatabase(oCurrentTreeNodeTag.DatabaseName);
            oSourceDb.ScriptOptions.RefreshData = bRefreshData;
            oWriter.Execute(ref oSourceDb, null);
            if (oWriter.HasErrors())
            {
                DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm oError =
                    new DBSourceToolsLib.SysObjects.UI.Forms.ErrorForm(oWriter.ErrorMessage);
                oError.ShowDialog(oForm);
            }


            //for (int i = 0; i < UserSettings.Instance().DatabaseProject.SourceDatabases.Count; i++)
            //{
            //    SourceDatabase oSource = UserSettings.Instance().DatabaseProject.SourceDatabases[i];
            //}
        }

    }
}
